package org.example.dto;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.example.constant.BaseConstant;
import org.example.form.PageForm;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.List;
import java.util.Set;

/**
 * 分页结果对象封装
 * @author joey
 */
@Data
@ApiModel(value="分页PageDto")
public class PageDto<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    @ApiModelProperty(value = "总页数")
    private Long pages;
    @ApiModelProperty(value = "总数量")
    private Long total;
    @ApiModelProperty(value = "列表对象")
    private List<T> list;
    @ApiModelProperty(value = "显示字段集合")
    private Set<String> columnList;
    @ApiModelProperty(value = "是否全显示")
    private boolean showAll = true;

    public static PageDto create(){
        PageDto dto = new PageDto();
        return dto;
    }

    public static PageDto create(Long pages, Long total, List list){
        PageDto dto = new PageDto();
        dto.setList(list);
        dto.setPages(pages);
        dto.setTotal(total);
        return dto;
    }
    public static PageDto create(Integer total, List list){
        PageDto dto = new PageDto();
        dto.setList(list);
        dto.setPages(1L);
        dto.setTotal(total.longValue());
        return dto;
    }
    /**
     * 获取分页对象
     * @param request
     * pageNo 当前页数 从1开始
     * pageSize 每页数
     * pageStatus  不为空时查询页数和总数，为空时不查询
     * @return
     */
    public static Page getPage(HttpServletRequest request){
        Long pageNo=getPageNo(request);
        Long pageSize=getPageSize(request);
        String pageStatus = request.getParameter("pageStatus");
        if(StringUtils.isBlank(pageStatus)){
            return new Page(pageNo,pageSize);
        }else {
            return new Page(pageNo,pageSize,false);
        }
    }
    public static Page getPage(PageForm form){
        if(form.getPageNo()==null){
            form.setPageNo(BaseConstant.INIT_PAGE_NO);
        }
        if(form.getPageSize()==null){
            form.setPageSize(BaseConstant.INIT_PAGE_SIZE);
        }
        if(StringUtils.isBlank(form.getPageStatus())){
            return new Page(form.getPageNo(),form.getPageSize());
        }else {
            return new Page(form.getPageNo(),form.getPageSize(),false);
        }
    }

    /**
     * 获取页数
     * @param request
     * @return
     */
    public static Long getPageNo(HttpServletRequest request){
        Long pageNo=1L;
        String pageNoStr = request.getParameter("pageNo");
        if(StringUtils.isNotBlank(pageNoStr)){
            pageNo=Long.parseLong(pageNoStr);
        }
        return pageNo;
    }

    /**
     * 获取每页显示数量
     * @param request
     * @return
     */
    public static Long getPageSize(HttpServletRequest request){
        Long pageSize=10L;
        String pageSizeStr = request.getParameter("pageSize");
        if(StringUtils.isNotBlank(pageSizeStr)){
            pageSize=Long.parseLong(pageSizeStr);
        }
        return pageSize;
    }


}
